package com.example.maybeaccount.Entry;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.maybeaccount.AddItem;
import com.example.maybeaccount.Mode.ActivityController;
import com.example.maybeaccount.Mode.SqliteDB;
import com.example.maybeaccount.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 图形密码解锁
 */
public class NineGridSetting extends AppCompatActivity implements LockPatternView.OnPatternChangeListener {

    private LockPatternView lpv;
    private TextView tv;
    private TextView textOthers;
    private int firstFlag = 0;
    private boolean isTop;
    private List<User> userList = new ArrayList<>();
    private User user = new User();
    private int inputTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBroadcast();
        setContentView(R.layout.ninegridunlock);

        lpv = (LockPatternView) findViewById(R.id.lock_pattern_view);
        tv = (TextView) findViewById(R.id.text);
        tv.setText("新密码");
        lpv.setOnPatternChangeListener(this);
        textOthers = findViewById(R.id.others);
        textOthers.setVisibility(View.GONE);
        textOthers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NineGridSetting.this,Entrance.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onPatternChange(String patternPassword) {
        if (patternPassword == null) {
            tv.setTextColor(Color.parseColor("#18b968"));
            tv.setText("请至少5个点");
        }else if(inputTime == 0) {
            lpv.clearDraw();
            tv.setText("请再画一次");
            user.setGridPwd(patternPassword);
            inputTime ++;
        } else if(inputTime == 1 && !user.getGridPwd().equals(patternPassword)) {
            lpv.clearDraw();
            tv.setText("不一致，再画一次");
            inputTime  = 0;
        } else if(inputTime == 1) {
            userList = SqliteDB.getInstance(getApplicationContext()).loadUser();
            user = userList.get(0);
            user.setGridPwd(patternPassword);
            if(SqliteDB.getInstance(getApplicationContext()).updateUserGridPswd(user) == 1){
                tv.setTextColor(Color.parseColor("#18b968"));
                //tv.setText("密码正确");
                tv.setText("设置成功");
                user.setGridPwd(patternPassword);
                Toast.makeText(this,"设置成功",Toast.LENGTH_LONG).show();
                //Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();
                //获取Intent，判断是否来自shortcut
                finish();


            }else{
                tv.setTextColor(Color.RED);
                tv.setText("设置失败");
            }
        }
    }

    @Override
    public void onPatternStarted(boolean isStarted) {
        if (isStarted) {
            tv.setTextColor(Color.parseColor("#18b968"));
            tv.setText("请绘制图案");
        }

    }

    /**
     * 后台唤醒后进入密码界面
     */
    @Override
    protected void onResume(){
        super.onResume();
        isTop = true;
        if(firstFlag == 1){
            firstFlag = 0;
            Intent intent = new Intent(NineGridSetting.this, Entrance.class);
            intent.putExtra("resume","Member");
            startActivity(intent);
        }else{
            firstFlag = 0;
        }
    }

    /** 广播监听home和menu */
    class InnerRecevier extends BroadcastReceiver {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {

                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);

                if (reason != null) {

                    if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY) && isTop) {
                        // "Home键被监听"
                        firstFlag = 1;

                    } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS) && isTop) {
                        //"多任务键被监听"
                        firstFlag = 1;

                    }
                }
            }
        }
    }

    private void initBroadcast(){
        /** 广播 */
        this.getWindow().setFlags(0x80000000,0x80000000);
        //创建广播
        InnerRecevier innerReceiver = new InnerRecevier();
        //动态注册广播
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        //启动广播
        registerReceiver(innerReceiver, intentFilter);
    }
}
